package com.dfc.springmvc.controller;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dfc.springmvc.common.JSONMsg;
import com.dfc.springmvc.pojo.Dictionary;
import com.dfc.springmvc.pojo.DictionaryDetail;
import com.dfc.springmvc.service.DictionaryDetailService;
import com.dfc.springmvc.service.DictionaryService;
import com.dfc.springmvc.util.StringUtil;

@Controller
@RequestMapping(value = "/dictionary")
public class DictionaryController extends BaseController {

	private Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private DictionaryService dictionaryService;

	@Autowired
	private DictionaryDetailService dictDetailService;

	@RequestMapping(value = "/initDictionary", method = RequestMethod.GET)
	public String loadDictionary() {
		return "/dictionary/dictionary";
	}

	@RequestMapping(value = "/queryDictionary", method = RequestMethod.POST)
	@ResponseBody
	public List<Dictionary> queryDictionary(Dictionary dictionary) {
		List<Dictionary> dictList = dictionaryService.queryDictionary(dictionary);
		return dictList;
	}

	@RequestMapping(value = "/getDict", method = RequestMethod.GET)
	@ResponseBody
	public JSONMsg getDict(String dictId) {
		Dictionary dictionary = dictionaryService.getDictionary(dictId);
		return JSONMsg.SUCCESS(dictionary);
	}

	@RequestMapping(value = "/saveOrUpdateDict", method = RequestMethod.POST)
	@ResponseBody
	public JSONMsg saveOrUpdateDict(Dictionary dictionary) {
		try {
			if (StringUtil.isNotEmpty(dictionary.getDictId())) {
				dictionaryService.updateDictionary(dictionary);
			} else {
				dictionary.setCreateTime(new Date());
				dictionary.setCreator(getUserInfo().getUserId());
				dictionaryService.insertDictionary(dictionary);
			}
			return JSONMsg.SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			return JSONMsg.FAILURE;
		}
	}

	@RequestMapping(value = "/updateDictStatus", method = RequestMethod.POST)
	@ResponseBody
	public JSONMsg updateDictStatus(Dictionary dictionary) {
		try {
			dictionaryService.updateDictionary(dictionary);
			return JSONMsg.SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			return JSONMsg.FAILURE;
		}
	}

	@RequestMapping(value = "/getDictByCode", method = RequestMethod.POST)
	@ResponseBody
	public JSONMsg getDictByCode(String dictCode) {
		Integer count = dictionaryService.getDictByCode(dictCode);
		return JSONMsg.SUCCESS(count);
	}

	@RequestMapping(value = "/initDictionaryDetail", method = RequestMethod.GET)
	public String initDictionaryDetail(Model model, String dictId, String dictCode) {
		model.addAttribute("dictId", dictId);
		model.addAttribute("dictCode", dictCode);
		return "/dictionary/dictionaryDetail";
	}

	@RequestMapping(value = "/getDictDetailByDictId", method = RequestMethod.POST)
	@ResponseBody
	public List<DictionaryDetail> getDictDetailByDictId(String dictId) {
		List<DictionaryDetail> dictDetailList = dictDetailService.getDictDetailByDictId(dictId);
		return dictDetailList;
	}

	@RequestMapping(value = "/getDictDetail", method = RequestMethod.GET)
	@ResponseBody
	public JSONMsg getDictDetail(String dictDetailId) {
		DictionaryDetail dictionaryDetail = dictDetailService.getDictDetailById(dictDetailId);
		return JSONMsg.SUCCESS(dictionaryDetail);
	}

	@RequestMapping(value = "/saveOrUpdateDictDetail", method = RequestMethod.POST)
	@ResponseBody
	public JSONMsg saveOrUpdateDictDetail(DictionaryDetail dictionaryDetail, String dictCode) {
		try {
			if (StringUtil.isNotEmpty(dictionaryDetail.getDictDetailId())) {
				dictDetailService.updateDictDetail(dictionaryDetail);
			} else {
				String dictDetailCode = dictDetailService.getNewDictDetailCode(dictionaryDetail.getDictId(), dictCode);
				dictionaryDetail.setDictDetailCode(dictDetailCode);
				dictionaryDetail.setCreateTime(new Date());
				dictionaryDetail.setCreator(getUserInfo().getUserId());
				dictDetailService.insertDictDetail(dictionaryDetail);
			}
			return JSONMsg.SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			return JSONMsg.FAILURE;
		}
	}

	@RequestMapping(value = "/updateDictDetailStatus", method = RequestMethod.POST)
	@ResponseBody
	public JSONMsg updateDictDetailStatus(DictionaryDetail dictionaryDetail) {
		try {
			dictDetailService.updateDictDetail(dictionaryDetail);
			return JSONMsg.SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			return JSONMsg.FAILURE;
		}
	}

}
